package com.guigu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.eduservice.dto.UserDto;
import com.guigu.eduservice.entity.Businessman;
import com.guigu.eduservice.entity.BusinessmanEquipment;
import com.guigu.eduservice.entity.Equipment;
import com.guigu.eduservice.mapper.BusinessmanMapper;
import com.guigu.eduservice.service.BusinessmanEquipmentService;
import com.guigu.eduservice.service.BusinessmanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.eduservice.service.EquipmentService;
import com.guigu.eduservice.vo.BusinessmanVo;
import org.springframework.beans.factory.annotation.Autowired;
import com.guigu.eduservice.utils.EmployeeJwtUtils;

import com.guigu.eduservice.utils.MD5;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 业务员表（含检测评估师，负责设备检测、求购跟进） 服务实现类
 * </p>
 *
 * @author janecraft
 * @since 2025-09-27
 */
@Service
public class BusinessmanServiceImpl extends ServiceImpl<BusinessmanMapper, Businessman> implements BusinessmanService {

    @Autowired
    private BusinessmanService businessmanService;

    @Autowired
    private  BusinessmanMapper businessmanMapper;

    @Autowired
   private EquipmentService equipmentService;

    @Autowired
    private BusinessmanEquipmentService businessmanEquipmentService;

    @Override
    public PageInfo<Businessman> getBusinessmanAll(BusinessmanVo vo, Integer pageNum, Integer pageSize) {
        QueryWrapper<Businessman> wrapper = new QueryWrapper<>();
        if(vo.getName()!="" && vo.getName()!=null){
            wrapper.like("name",vo.getName());
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Businessman> list = businessmanService.list(wrapper);
        PageInfo<Businessman> objectPageInfo = new PageInfo<>(list);

        return objectPageInfo;
    }
    @Override
    public String EmployeeLogin(UserDto userDto) {
        //
        QueryWrapper<Businessman> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",userDto.getPhone());
        Businessman one = this.getOne(queryWrapper);
        if(one == null)
            return "用户不存在";
        //判断用户输入的密码是否正确
        if(!MD5.encrypt(userDto.getPassword()).equals(one.getPassword()))
           return "密码错误";
        if(one.getStatus()==0)
            return "用户被禁用";
        //生成jwt令牌：认证成功的用户对象的id, nickname
        String jwtToken = EmployeeJwtUtils.getJwtToken(one.getBusinessmanId(), one.getName(),one.getDepartment());
        return jwtToken;
    }

    //根据操作员的id查询业务员
    @Override
    public Businessman getOperatorId(Integer id) {
        Businessman operatorId = businessmanMapper.getOperatorId(id);
        return operatorId;
    }

    //根据业务员的id查询设备表和业务员关联的表，再根据关联表里的设备Id查询业务员所有归属的设备
    @Override
    public List<Equipment> getBusinessmanEquipmentByBusinessmanId(Long BusinessmanId) {
        List<Equipment> EquipmentList = new ArrayList<>();
        QueryWrapper<BusinessmanEquipment> wrapper = new QueryWrapper<>();
        if (BusinessmanId!=null){
            wrapper.eq("businessman_id",BusinessmanId);
        }
        //根据业务员的id查询关联表
        List<BusinessmanEquipment> list = businessmanEquipmentService.list(wrapper);
        for (BusinessmanEquipment equipment : list) {
            Equipment byId = equipmentService.getById(equipment.getEquipmentId());
            EquipmentList.add(byId);
        }
        return EquipmentList;
    }
    //根据业务员的id查询他分配了多少台设备
    @Override
    public Integer getCountByBusinessman_id(Long businessman_id) {
        Integer countByBusinessmanId = businessmanMapper.getCountByBusinessman_id(businessman_id);
        return countByBusinessmanId;
    }
}
